# encoding=utf-8
import os
import time
import logging

logger_ndict = {"info": logging.INFO, "error": logging.ERROR,
                "warning": logging.WARNING, "debug": logging.DEBUG}


class dlogger(object):
    def __init__(self, log_path=None, app_name='default',
                 print_level='error', write_level='debug'):
        # 文件的命名
        if not log_path:
            log_path = './logs'
        if not os.path.exists(log_path):
            os.makedirs(log_path)

        self.logname = os.path.join(log_path, '%s-%s.log' % (app_name, time.strftime('%Y_%m_%d')))
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        # 日志输出格式
        self.formatter = logging.Formatter("[%(asctime)s] - " + app_name + "] - %(levelname)s: %(message)s")
        self.print_level = logger_ndict[print_level]
        self.write_level = logger_ndict[write_level]

    def __console(self, level, message):
        # 创建一个FileHandler，用于写到本地
        fh = logging.FileHandler(self.logname, 'a', encoding='utf-8')  # 这个是python3的
        fh.setLevel(self.write_level)
        fh.setFormatter(self.formatter)
        self.logger.addHandler(fh)

        # 创建一个StreamHandler,用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(self.print_level)
        ch.setFormatter(self.formatter)
        self.logger.addHandler(ch)

        if level == 'info':
            self.logger.info(message)
        elif level == 'debug':
            self.logger.debug(message)
        elif level == 'warning':
            self.logger.warning(message)
        elif level == 'error':
            self.logger.error(message)
        # 这两行代码是为了避免日志输出重复问题
        self.logger.removeHandler(ch)
        self.logger.removeHandler(fh)
        # 关闭打开的文件
        fh.close()

    def debug(self, message):
        self.__console('debug', message)

    def info(self, message):
        self.__console('info', message)

    def warning(self, message):
        self.__console('warning', message)

    def error(self, message):
        self.__console('error', message)


if __name__ == '__main__':
    log = dlogger("demo")
    log.info("good")
